﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScheduleMaker.DB;
using ScheduleMaker.DB.CoreDAO;
using ScheduleMaker.ConfigManager;

namespace ScheduleMaker
{
    public partial class smAdmTeacherEditForm : Form
    {
        //DB.Domain.TeacherSchool teacher;
        DB.Domain.Core.Teacher teacher;
        // Przedmioty w placówce
        //IList<DB.Domain.SubjectSchool> subjects;
        IList<DB.Domain.Core.Subject> subjects;

        public smAdmTeacherEditForm(DB.Domain.Core.Teacher _teacher)
        {
            InitializeComponent();

            this.teacher = _teacher;
            lblTeacherName.Text = teacher.Firstname + " " + teacher.Surname + " - " + teacher.Name;

            this.getSubjectList();
            this.teacherSubjectList();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Czy na pewno chcesz usunąć wybrany przedmiot?", "Potwierdź usunięcie", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // coś tam xD - gdy usuwamy
                int selectedIndex = lbTeachersSubjects.SelectedIndex;
                try {
                    TeacherDAO teacherDAO = DAOFactory.getTeacherDAO(ConfigManager.ConfigManager.getConfiguration().Type);

                    if (ConfigManager.ConfigManager.getConfiguration().Type == Mode.School)
                    {
                        DB.Domain.Core.Subject subject = teacher.Subjects.ElementAt(selectedIndex);
                        teacher.Subjects.Remove(subject);
                    }
                    else
                    {
                        DB.Domain.Core.Subject subject = teacher.SubjectCount.Keys.ElementAt(selectedIndex);
                        teacher.SubjectCount.Remove(subject);
                    }

                    teacherDAO.saveOrUpdate(teacher);
                    // odświeżenie listy
                    this.teacherSubjectList();
                }
                catch
                {
                    MessageBox.Show("Nie wybrano przedmiotu!", "Błąd " + selectedIndex);
                }
            }
        }


        /**
         *  TODO - zrobić dodawanie przedmiotów
         */ 
        private void button1_Click(object sender, EventArgs e)
        {

            int selectedIndex = lbSubjects.SelectedIndex;
            try
            {
                // Nowe
                DB.Domain.Core.Subject subject = subjects.ElementAt(selectedIndex);
                SubjectDAO subjectDAO = DAOFactory.getSubjectDAO(ConfigManager.ConfigManager.getConfiguration().Type);
                

                if (!teacher.Subjects.Contains(subject))
                {
                    // DLA SZKOŁY
                    if (ConfigManager.ConfigManager.getConfiguration().Type == Mode.School)
                    {
                        TeacherDAO teacherDAO = DAOFactory.getTeacherDAO(ConfigManager.ConfigManager.getConfiguration().Type);
                        teacher.Subjects.Add(subject);
                        teacherDAO.saveOrUpdate(teacher);
                    }
                        // DLA UNIWERSYTETU
                    else
                    {
                        TeacherDAO teacherDAO = DAOFactory.getTeacherDAO(ConfigManager.ConfigManager.getConfiguration().Type);

                        int numberGroups = 0;
                        using (smAdminSubjectAddHour subjectAddHourForm = new smAdminSubjectAddHour(1))
                        {
                            subjectAddHourForm.ShowDialog();
                            numberGroups = subjectAddHourForm.numberHours;
                        }
                        teacher.SubjectCount.Add(subject, numberGroups);

                        teacherDAO.saveOrUpdate(teacher);
                    }


                }
                else
                {
                    MessageBox.Show("Przedmiot już dodano!", "Błąd " + selectedIndex);
                }
                
                //this.getUserList();
                this.teacherSubjectList();
                

            }
            catch
            {
                MessageBox.Show("Nie wybrano przedmiotu!", "Błąd " + selectedIndex);
            }



            /*
                         int selectedIndex = lbSubjects.SelectedIndex;
                        int numberHours = 0;

                        try
                        {
                            DB.Domain.Core.Subject subject = subjects.ElementAt(selectedIndex);
                            SubjectDAO subjectDAO = DAOFactory.getSubjectDAO(ConfigManager.ConfigManager.getConfiguration().Type);

                            if (!group.SubjectsCount.Keys.Contains(subject))
                            {
                                GroupDAO groupDAO = DAOFactory.getGroupDAO(ConfigManager.ConfigManager.getConfiguration().Type);

                                using (smAdminSubjectAddHour subjectAddHourForm = new smAdminSubjectAddHour(1))
                                {
                                    subjectAddHourForm.ShowDialog();
                                    numberHours = subjectAddHourForm.numberHours;
                                }

                                // Dodanie przedmiotu wraz z ilością godzin
                                group.SubjectsCount.Add(subject, numberHours);

                                //group.Subjects.Add(subject);

                                groupDAO.saveOrUpdate(group);
                            }
                            else
                            {
                                MessageBox.Show("Przedmiot już dodano!", "Błąd " + selectedIndex);
                            }
                            //this.getUserList();
                            this.getGroupSubjectList();
                        }
                        catch
                        {
                            MessageBox.Show("Nie wybrano przedmiotu!", "Błąd " + selectedIndex);
                        }
             * 
             */
        }

        private void smAdmTeacherEditForm_Load(object sender, EventArgs e)
        {

        }

        // Pobieranie - przedmiotów
        private void getSubjectList()
        {
            lbSubjects.Items.Clear();
            //DB.SubjectDAOSchool subjectDAO = new DB.SubjectDAOSchool();
            SubjectDAO subjectDAO = DAOFactory.getSubjectDAO(ConfigManager.ConfigManager.getConfiguration().Type);

            subjects = subjectDAO.getAll();
            foreach (var elem in subjects)
            {
                lbSubjects.Items.Add(elem.Id + ": " + elem.Name);
                //lbSubjects.Items.Add("id: " + elem.Id + " Name: " + elem.Name);
                //Console.WriteLine("User id: " + elem.Id + " Login: " + elem.Login);
            }
        }

        private void teacherSubjectList()
        {
            lbTeachersSubjects.Items.Clear();

            if (ConfigManager.ConfigManager.getConfiguration().Type == Mode.School)
            {
                foreach (var elem in teacher.Subjects)
                {
                    lbTeachersSubjects.Items.Add(elem.Id + ": " + elem.Name);
                    //lbTeachersSubjects.Items.Add("id: " + elem.Id + " Name: " + elem.Name);
                }
            }
            else
            {
                foreach (var elem in teacher.SubjectCount)
                {
                    lbTeachersSubjects.Items.Add(elem.Key.Id + ": " + elem.Key.Name + " grup: " + elem.Value);
                    //lbTeachersSubjects.Items.Add("id: " + elem.Id + " Name: " + elem.Name);
                }
            }
        }


    }
}
